from django.contrib.auth.hashers import make_password

from . import forms
from .models import CustomUser
from django.http import HttpResponse
from django.shortcuts import render, redirect
from django.urls import reverse
from django.contrib.auth.views import auth_login, auth_logout, login_required


# Create your views here.
def login(request):
    login_form = forms.LoginForm()
    # login_form = None
    error = None
    all_content = CustomUser.objects.values_list('username', flat=True)
    all_contents = []
    for content in all_content:
        all_contents.append(content)
    # print(all_contents)
    if request.method == "POST":
        login_form = forms.LoginForm(data=request.POST)
        username = request.POST.get("username")
        password = request.POST.get("password")
        user = CustomUser.objects.filter(username=username).first()
        if user and user.check_password(password):
            auth_login(request, user)
            next_url = request.GET.get("next")
            if next_url:
                return redirect(to=next_url)
            else:
                return redirect(to=reverse("main:jdg_j"))
        else:
            error = "账号或密码错误"
    return render(request, "user/login.html",
                  context={"error": error, "login_form": login_form, "all_contents": all_contents, })


def logout(request):
    auth_logout(request)
    return redirect(to=reverse("main:jdg_j"))


def register(request):
    error = None
    register_form = forms.RegisterForm()
    # print(register_form)
    all_content = CustomUser.objects.values_list('username', flat=True)
    all_contents = []
    for content in all_content:
        all_contents.append(content)
    if request.method == "POST":
        register_form = forms.RegisterForm(request.POST)
        username = request.POST.get("username")
        if register_form.is_valid():
            password1 = request.POST.get("password")
            password2 = request.POST.get("password2")
            if password1 != password2:
                error = "两次密码不一致"
            else:
                user = register_form.save(commit=False)
                user.password = make_password(password1)
                user.save()
                auth_login(request, user)
                return redirect(to=reverse("user:login"))
        elif username in all_contents:
            error = "账号已存在，看不见叉号吗！"
    res = render(request, "user/register.html",
                 context={"register_form": register_form, "all_contents": all_contents, "error": error})
    return res


@login_required(login_url="/user/login",)
def center(request):
    cf = forms.CenterForm(initial={
        "username": request.user.username,
        "email": request.user.email,
        "telephone": request.user.telephone,
        "first_name": request.user.first_name,
        "last_name": request.user.last_name,
    })
    if request.method == "POST":
        cf = forms.CenterForm(data=request.POST, files=request.FILES, instance=request.user)
        if cf.is_valid():
            cf.save()
            return redirect(to=reverse("main:jdg_j"))
    return render(request, "user/center.html", context={"cf": cf})


@login_required(login_url="/user/login",)
def updatepwd(request):
    upf = forms.UpdatePwdForm()
    errors = None
    if request.method == "POST":
        upf = forms.UpdatePwdForm(request.POST)
        password1 = request.POST.get("password1")
        password2 = request.POST.get("password2")
        password3 = request.POST.get("password3")
        if request.user.check_password(password1):
            if password2 != password3:
                errors = "两次新密码不一致"
            else:
                request.user.set_password(password2)
                request.user.save()
                auth_logout(request)
                return redirect(to=reverse("user:login"))
        else:
            errors = "原始密码不对"
    return render(request, "user/updatepwd.html", context={"upf": upf, "errors": errors, })

